import {
	login,
	getInfo
} from '../../api/login/user.js'
import {
	showInfo
} from '../../api/user/attestation.js'
const state = {
	//是否强制登录
	forcedLogin:false,
	// 用户ID
	userID: '4',
	// 用户头像
	avatar: '',
	// 用户名
	nickName: '',
	//用户电话
	phone: 15023421665,
	//性别
	gender: 2,
	//生日
	birthday: '2022-09-02',
	//身份证号
	idNumber: '',
	// 是否登录
	isLogin: false,
	// 用户角色
	role: '',
	name: '',
	// 司机编号
	driverCode: '',
	// 车牌
	carCode: '',
	// 是否处于运输中状态
	isTransport: false,
	//认证状态
	carState: '4',
	//认证姓名
	carName: '',
	//认证身份证
	carIdCard: '',
	//认证图片
	carPicture: '',
	carRearon: ' ',
	//认证编码
	code: '',
	carId:'',
};
const mutations = {
	login: (state, res) => {
		state.isLogin = res;
	},
	userState: (state, res) => {
		state.userID = res.user.userId;
		state.avatar = res.user.userPicture;
		state.name = res.user.name;
		state.nickName = res.user.nickName;
		state.phone = res.user.phone;
		state.gender = res.user.gender;
		state.birthday = res.user.birthday;
		state.role = res.user.role;
		state.driverCode = res.user.driverCode;
		state.idNumber = res.user.idNumber;
	},
	attestationState: (state, res) => {
		state.carId=res.id;
		state.code = res.code;
		state.carState = res.state;
		state.carName = res.name;
		state.carIdCard = res.idNumber;
		state.carPicture = res.carPicture;
		state.carRearon = res.reason;
	},

	driverState: (state, res) => {
		// 给司机用户赋值
		state.driverCode = res.user.driverCode;
	},
	setUserInfo: (state, data) => {
		state.name = data.name;
		state.idNumber = data.idNumber;
	},
	setUser: (state, data) => {
		state.avatar = data.userPicture
		state.nickName = data.nickName;
		state.gender = data.gender;
		state.birthday = data.birthday;
	},
	outState:(state,data)=>{
		state.avatar = '';
	},
	nullAttestation:(state)=>{
		state.carId='';
		state.carState = '';
		state.code = '';
		state.carState = '';
		state.carName = '';
		state.carIdCard = '';
		state.carPicture = '';
		state.carRearon = '';
	}

}
const actions = {
	DRIVERINFO({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			showInfo(data).then(res => {
				console.log(JSON.stringify(res))
				if (res.header.code == 200&&res.body!=null) {
					console.log(res);
					uni.setStorageSync('attestation', res.body); //存入缓存
					commit('attestationState', res.body);
					resolve(res.header)
				}else{
					console.log("res");
					commit('nullAttestation');
					resolve(res.header)
				}
			}).catch(err => {
				reject(err)
			})
		})
	},

	LOGIN({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			login(data).then(res => {
					if (res.header.code == 200) {
						uni.showToast({
							icon: 'success',
							title: '登录成功',
							duration: 2000
						});
						try {
							uni.setStorageSync('token', res.body); //存入缓存
						} catch (e) {
							// error
						}
						commit('login', true);

						getInfo().then(res1 => {
							if (res.header.code == 200) {
								let user = {
									user: res1.body
								}
								try {
									uni.setStorageSync('user', user); //存入缓存
								} catch (e) {
									console.log(e);
									// error
								}
								commit('userState', user);
								//这里应该判断用户身份，是司机用户则还需要提交到driverState
								if (user.user.role === 'driver') {
									console.log(123);
									commit('driverState', user);
								}
							}
						})
						resolve(res.header)
					} else if (res.code === 902) {
						uni.showToast({
							icon: 'none',
							duration: 2000,
							title: res.body
						});
					} else {
						uni.showToast({
							icon: 'none',
							duration: 2000,
							title: res.body
						});
					}
				})
				.catch(err => {
					reject(err)
				})
		})
	},

	USERSTATE({
		commit
	}, res) {
		commit('userState', res);
	},

	SETUSERINFO({
		commit
	}, res) {
		console.log(res);
		commit('setUserInfo', res);
	},
	UPDATEINFO({
		commit
	}, res) {
		commit('setUser', res);
	},
	GETLOGIN({
		commit
	}, data) {
		console.log(data);
		if(data !=''){
			commit('login', true);
			commit('userState', data);
		}else{
			commit('login', false);
			commit('outState', null);
		}
	}

}
export default {
	namespaced: true,
	mutations,
	actions,
	state
}
